﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;


namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(true)]
    [ToolboxData("<{0}:Selectable Selector=\"\" runat=server />")]
    public class Selectable : BaseInteractionExtender
    {
        public Selectable()
        {
            Interaction = "selectable";
        }

        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSelected
        {
            get
            {
                if (ViewState["OnClientSelected"] == null) return "";
                return (string)ViewState["OnClientSelected"];
            }
            set
            {
                ViewState["OnClientSelected"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSelecting
        {
            get
            {
                if (ViewState["OnClientSelecting"] == null) return "";
                return (string)ViewState["OnClientSelecting"];
            }
            set
            {
                ViewState["OnClientSelecting"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSelectStart
        {
            get
            {
                if (ViewState["OnClientSelectStart"] == null) return "";
                return (string)ViewState["OnClientSelectStart"];
            }
            set
            {
                ViewState["OnClientSelectStart"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientSelectStop
        {
            get
            {
                if (ViewState["OnClientSelectStop"] == null) return "";
                return (string)ViewState["OnClientSelectStop"];
            }
            set
            {
                ViewState["OnClientSelectStop"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientUnselected
        {
            get
            {
                if (ViewState["OnClientUnselected"] == null) return "";
                return (string)ViewState["OnClientUnselected"];
            }
            set
            {
                ViewState["OnClientUnselected"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientUnselecting
        {
            get
            {
                if (ViewState["OnClientUnselecting"] == null) return "";
                return (string)ViewState["OnClientUnselecting"];
            }
            set
            {
                ViewState["OnClientUnselecting"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientItemStyle
        {
            get
            {
                if (ViewState["ClientItemStyle"] == null) return "";
                return (string)ViewState["ClientItemStyle"];
            }
            set
            {
                ViewState["ClientItemStyle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientListStyle
        {
            get
            {
                if (ViewState["ClientListStyle"] == null) return "";
                return (string)ViewState["ClientListStyle"];
            }
            set
            {
                ViewState["ClientListStyle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientSelectedStyle
        {
            get
            {
                if (ViewState["ClientSelectedStyle"] == null) return "";
                return (string)ViewState["ClientSelectedStyle"];
            }
            set
            {
                ViewState["ClientSelectedStyle"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ClientSelectingStyle
        {
            get
            {
                if (ViewState["ClientSelectingStyle"] == null) return "";
                return (string)ViewState["ClientSelectingStyle"];
            }
            set
            {
                ViewState["ClientSelectingStyle"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ClientSelectable
        {
            get
            {
                if (ViewState["ClientSelectable"] == null) return false;
                return (bool)ViewState["ClientSelectable"];
            }
            set
            {
                ViewState["ClientSelectable"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoRefresh
        {
            get
            {
                if (ViewState["AutoRefresh"] == null) return false;
                return (bool)ViewState["AutoRefresh"];
            }
            set
            {
                ViewState["AutoRefresh"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Cancel
        {
            get
            {
                if (ViewState["Cancel"] == null) return "";
                return (string)ViewState["Cancel"];
            }
            set
            {
                ViewState["Cancel"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Delay
        {
            get
            {
                if (ViewState["Delay"] == null) return 0;
                return (int)ViewState["Delay"];
            }
            set
            {
                ViewState["Delay"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int Distance
        {
            get
            {
                if (ViewState["Distance"] == null) return 0;
                return (int)ViewState["Distance"];
            }
            set
            {
                ViewState["Distance"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Filter
        {
            get
            {
                if (ViewState["Filter"] == null) return "";
                return (string)ViewState["Filter"];
            }
            set
            {
                ViewState["Filter"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public ToleranceOptions Tolerance
        {
            get
            {
                if (ViewState["Tolerance"] == null) return ToleranceOptions.touch;
                return (ToleranceOptions)ViewState["Tolerance"];
            }
            set
            {
                ViewState["Tolerance"] = value;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            AddBinding("selected", OnClientSelected);
            AddBinding("selecting", OnClientSelecting);
            AddBinding("start", OnClientSelectStart);
            AddBinding("stop", OnClientSelectStop);
            AddBinding("unselected", OnClientUnselected);
            AddBinding("unselecting", OnClientUnselecting);

            if (!AutoRefresh) base.AddParameter("autoRefresh", "false");
            if (Cancel != "") base.AddParameter("cancel", "'" + Cancel + "'");
            if (Delay != 0) base.AddParameter("delay", Delay.ToString());
            if (Distance != 0) base.AddParameter("distance", Distance.ToString());
            if (Filter != "") base.AddParameter("filter", "'" + Filter + "'");
            if (Tolerance != ToleranceOptions.touch) base.AddParameter("tolerance", "'" + Tolerance.ToString() + "'");
            

            writer.Write("<STYLE type=text/css>");
            if (ClientSelectingStyle != "") writer.Write(ResolvedSelector + " .ui-selecting {" + ClientSelectingStyle + " }");
            if (ClientSelectedStyle != "") writer.Write(ResolvedSelector + " .ui-selected { " + ClientSelectedStyle + " }");
            if (ClientListStyle != "") writer.Write(ResolvedSelector + " { list-style-type: " + ClientListStyle + " }");
            if (ClientItemStyle != "") writer.Write(ResolvedSelector + " li { " + ClientItemStyle + " }");
            writer.Write("</STYLE>");

            base.Render(writer);
        }
    }
}
